<?php 
defined('RUN_KATANA') or die('Restricted access');
require(KATANA_CORES_INCLUDE_PATH.'fleet/ScheduleItem.php');
require(KATANA_CORES_INCLUDE_PATH.'fleet/Ship.php');
class Fleet extends ObjectController
{
	public 	$SiteId;
	public 	$ShipList;
	public 	$CurrentShip;
	public 	$CurrentShipId;
	public 	$CurrentShipIdNumber;
	
	public function Fleet($shipId=0, $shipIdNumber=0, $view='') 
	{ 
		global $katana;
		self::setSiteId($katana->getId());
		self::setView($view);
		if(intval($shipId) > 0)
		{ self::setCurrentShipId($shipId); }
		if(intval($shipIdNumber) > 0)
		{ self::setCurrentShipIdNumber($shipIdNumber); }
		if($this->CurrentShipIdNumber > 0)
		{ self::loadCurrentShip(); }
	}
	
	public function getSiteId() { return $this->SiteId; } 
	public function getShipList() 
	{ 
		self::loadShipList();
		return $this->ShipList; 
	}
	public function getCurrentShip(){ return $this->CurrentShip;  }
	public function getCurrentShipId() { return $this->CurrentShipId; }
	public function getCurrentShipIdNumber() { return $this->CurrentShipIdNumber; }
	
	/* Set Functions */ 
	public function setSiteId($val) { $this->SiteId=intval($val); } 
	public function setShipList($val) { $this->ShipList=$val; }
	public function setCurrentShip($val) { $this->CurrentShip=$val; }
	public function setCurrentShipId($val) { $this->CurrentShipId=intval($val); }
	public function setCurrentShipIdNumber($val) { $this->CurrentShipIdNumber=intval($val); }
	
	function loadCurrentShip()
	{
		if(($this->CurrentShipId > 0) || ($this->CurrentShipIdNumber > 0))
		{			
			$select  = 's.id, ';
			$select .= 's.site_id, ';
			$select .= 's.parent_id, ';
			$select .= 's.identity_number, ';
			$select .= 's.name , ';
			$select .= 's.ship_order, ';
			$select .= 's.active, ';
			$select .= 's.date_created, ';
			$select .= 's.date_modified ';
			if($this->CurrentShipId > 0)
			{ $sql  = 'SELECT '.$select.' FROM '.DB_PERFIX.'fleet_ship AS s WHERE s.id='.$this->CurrentShipId.' LIMIT 1;'; }
			else
			{ $sql  = 'SELECT '.$select.' FROM '.DB_PERFIX.'fleet_ship AS s WHERE s.identity_number='.$this->CurrentShipIdNumber.' LIMIT 1;'; }
			
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ 
					self::setCurrentShipId($row['id']);
					self::setCurrentShipIdNumber($row['identity_number']);
					$this->CurrentShip = new Ship;
					$this->CurrentShip->setView($this->View);
					$this->CurrentShip->setId($row['id']);
					$this->CurrentShip->setParentId($row['parent_id']);
					$this->CurrentShip->setIdentityNumber($row['identity_number']);
					$this->CurrentShip->setName($row['name']);
					$this->CurrentShip->setShipOrder($row['ship_order']);
					$this->CurrentShip->setDateCreated($row['date_created']);
					$this->CurrentShip->setDateModified($row['date_modified']);
					$this->CurrentShip->setActive($row['active']);
					$this->CurrentShip->loadScheduleItems();
				}
			}
		}
	}
	
	function loadShipList($orderby='')
	{
		$i=0;
		if($this->SiteId > 0)
		{
			self::setItemsLoaded(true);
			
			$select  = 's.id, ';
			$select .= 's.site_id, ';
			$select .= 's.parent_id, ';
			$select .= 's.identity_number, ';
			$select .= 's.name , ';
			$select .= 's.ship_order, ';
			$select .= 's.active, ';
			$select .= 's.date_created, ';
			$select .= 's.date_modified ';
	
			$sql  = ' SELECT '.$select.' FROM '.DB_PERFIX.'fleet_ship AS s  ';
			if(self::getView() == 'frontend_list')
			{ $sql .= ' WHERE s.active=1 '; }
			$sql .= ' ORDER BY s.ship_order ASC';
			$sql .= ';';
			
			$result = mysql_query($sql); 
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					//print_r($row);
					$this->ShipList[$row['id']] = new Ship;
					$this->ShipList[$row['id']]->setId($row['id']);
					$this->ShipList[$row['id']]->setParentId($row['parent_id']);
					$this->ShipList[$row['id']]->setIdentityNumber($row['identity_number']);
					$this->ShipList[$row['id']]->setName($row['name']);
					$this->ShipList[$row['id']]->setShipOrder($row['ship_order']);
					$this->ShipList[$row['id']]->setDateCreated($row['date_created']);
					$this->ShipList[$row['id']]->setDateModified($row['date_modified']);
					$this->ShipList[$row['id']]->setActive($row['active']);
					
					if($this->CurrentShipId == $row['id'])
					{
						$this->CurrentShip = new Ship;
						$this->CurrentShip->setId($row['id']);
						$this->CurrentShip->setParentId($row['parent_id']);
						$this->CurrentShip->setIdentityNumber($row['identity_number']);
						$this->CurrentShip->setName($row['name']);
						$this->CurrentShip->setShipOrder($row['ship_order']);
						$this->CurrentShip->setDateCreated($row['date_created']);
						$this->CurrentShip->setDateModified($row['date_modified']);
						$this->CurrentShip->setActive($row['active']);
						$this->CurrentShip->loadScheduleItems();
					}
				}
			}
		}
	}
	
	function addShip($name='', $identity='')
	{
		$name = self::formatText($name);
		$identity = self::formatInt($identity);
		if(($name != '') || ($identity != ''))
		{
			
			$neworder = 0;
			$result = mysql_query('SELECT ship_order FROM '.DB_PERFIX.'fleet_ship WHERE parent_id=0 AND site_id='.$this->SiteId.' ORDER BY ship_order DESC LIMIT 1;');
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ $neworder = $row['ship_order']; }
				$neworder++;
			}
			$into = '`site_id` ,';
			$vals = $this->SiteId.', ';
			
			$into .= '`identity_number`, ';
			$vals .= $identity.', ';
			
			$into .= '`name`, ';
			$vals .= '"'.$name.'", ';
			
			$into .= '`ship_order`, ';
			$vals .= $neworder.', ';
			
			$into .= '`date_modified`, ';
			$vals .= 'NOW(), ';
			
			$into .= '`date_created`';
			$vals .= 'NOW()';
			
			$sql  = 'INSERT INTO '.DB_PERFIX.'fleet_ship ('.$into.') VALUES ('.$vals.');';
			$result = mysql_query($sql);
		}
	}
	
	function deleteShip($id)
	{
		if($id > 0)
		{
			if(isset($this->ShipList[$id]))
			{
				$delete = 'DELETE FROM '.DB_PERFIX.'fleet_ship_schedule WHERE ship_id='.$id.' LIMIT 1;';
				$result = mysql_query($delete);
				$delete = 'DELETE FROM '.DB_PERFIX.'fleet_ship WHERE id='.$id.' LIMIT 1;';
				$result = mysql_query($delete);
				unset($this->ShipList[$id]);
				return true;
			}
		}
	}
	
	public function addScheduleItem($port, $country='--', $eta='', $etah=0, $etam=0, $etd='', $etdh=0, $etdm=0)
	{
		if($this->CurrentShipId > 0)
		{
			$port = self::formatText($port);
			if(($port != '') && (strlen($country) == 2) && (strlen($eta) > 9) && (strlen($etd) > 9))
			{
				$etaTime = mktime(intval($etah), intval($etam), 0, abs(substr($eta, 5, 2)), abs(substr($eta, 8, 2)),  substr($eta, 0, 4));
				$etdTime = mktime(intval($etdh), intval($etdm), 0, abs(substr($etd, 5, 2)), abs(substr($etd, 8, 2)),  substr($etd, 0, 4));

				$into = '`ship_id` ,';
				$vals = $this->CurrentShipId.', ';
				
				$into .= '`port`, ';
				$vals .= '"'.$port.'", ';
				
				$into .= '`country`, ';
				$vals .= '"'.$country.'", ';
				
				$into .= '`eta`, ';
				$vals .= '"'.date("Y-m-d H:i:s",$etaTime).'", ';
				
				$into .= '`etd`';
				$vals .= '"'.date("Y-m-d H:i:s",$etdTime).'"';
				
				$sql  = 'INSERT INTO '.DB_PERFIX.'fleet_ship_schedule ('.$into.') VALUES ('.$vals.');';
				$result = mysql_query($sql);
			}
		}
	}
	
	function deleteScheduleItem($id)
	{
		if($id > 0)
		{
			if(isset($this->CurrentShip->ScheduleItems[$id]))
			{
				$delete = 'DELETE FROM '.DB_PERFIX.'fleet_ship_schedule WHERE ship_id='.$this->CurrentShip->getId().' AND id='.$id.' LIMIT 1;';
				$result = mysql_query($delete);
				if($result)
				{
					unset($this->CurrentShip->ScheduleItems[$id]);
					return true;
				}
			}
		}
	}
	
	public function cronHour()
	{ 
		if(defined('RUN_CRON'))
		{
			self::loadShipList();
			$out = '';
			if(count($this->ShipList) > 0)
			{
				foreach($this->ShipList as $ship)
				{
					$sql = 'SELECT * FROM '.DB_PERFIX.'fleet_ship_schedule WHERE ship_id='.$ship->getId().' AND eta<"'.date("Y-m-d H:i:s").'" AND archived=0 ORDER BY eta DESC';
					$result = mysql_query($sql); 
					if($result)
					{
						$i=0;
						while($row = mysql_fetch_array($result))
						{
							if($i > 0)
							{
								$update = 'UPDATE '.DB_PERFIX.'fleet_ship_schedule SET archived=1 WHERE id='.$row['id'];
								mysql_query($update);
								$out .= 'ARCHIVED:ship_schedule ID:'.$row['id']."<br />";
							}
							$i++;
						}
					}
					
					$sql = 'SELECT * FROM '.DB_PERFIX.'fleet_ship_schedule WHERE ship_id='.$ship->getId().' AND eta>"'.date("Y-m-d H:i:s").'" AND archived=1 ORDER BY eta DESC';
					$result = mysql_query($sql); 
					if($result)
					{
						while($row = mysql_fetch_array($result))
						{
							$update = 'UPDATE '.DB_PERFIX.'fleet_ship_schedule SET archived=0 WHERE id='.$row['id'];
							mysql_query($update);
							$out .= 'RESTORED FROM ARCHIVE:ship_schedule ID:'.$row['id']."<br />";
						}
					}
				}
			}
			return $out;
		}
	}
}
?>